package com.lyd.productanalyse.controller;

import com.alibaba.fastjson2.JSON;
import com.lyd.productanalyse.entity.EchartResultDomain;
import com.lyd.productanalyse.entity.ExcelAnalyseVO;
import com.lyd.productanalyse.entity.ExcelCalResultDomain;
import com.lyd.productanalyse.service.ExcelReaderService;
import com.lyd.productanalyse.service.FilterDataService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.*;

/**
 * 描述 产品分析控制类
 *
 * @author liyadong
 * @created 2023-11-20 10:12
 */
@Controller
@RequestMapping("/analyse")
public class ProductAnalyseController {
    /**
     * 读取excel文件
     */
    @Resource
    private ExcelReaderService excelReaderService;

    /**
     * 过滤数据服务
     */
    @Resource
    private FilterDataService filterDataService;

    /**
     *
     * @return java.lang.String
     * @author liyadong
     * @date 2023/11/20 10:13
     */
    @RequestMapping("/chart")
    public String analyse() {
        return "analyse";
    }

    /**
     * 查询分析数据
     * @param	excelAnalyseVO 查询参数
     * @author  liyadong
     * @date    2023/11/21 9:24
     * @return  com.lyd.productanalyse.entity.EchartResultDomain
     */
    @RequestMapping(value = "/data")
    @ResponseBody
    public EchartResultDomain findAll(@RequestBody ExcelAnalyseVO excelAnalyseVO){
        String percent = excelAnalyseVO.getPercent();

        // 解析excel文件
        ExcelCalResultDomain excelCalResultDomain =
                excelReaderService.readExcel(excelAnalyseVO.getFileName(), excelAnalyseVO.getSheetIndex(), excelAnalyseVO.getGroupField());
        // 根据比例过滤数据
        Map<String, BigDecimal> filter = filterDataService.filter(percent, excelCalResultDomain);
        // 组装返回数据
        return filterDataService.handleData(percent,excelCalResultDomain.getTotal(), filter);
    }


    @RequestMapping(value = "/list/head")
    @ResponseBody
    public List<String> findAll(){
       return Arrays.asList("00", "", "");
    }

}
